package hello;

import hello.domain.*;
import hello.repo.CustomerRepository;
import hello.repo.OrderRepository;
import hello.repo.ProductRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

import java.util.Optional;

@EnableJpaAuditing
@SpringBootApplication
public class Application {

    private static final Logger log = LoggerFactory.getLogger(Application.class);

    public static void xmain(String[] args) {
        SpringApplication.run(Application.class);
    }

    @Bean
    public CommandLineRunner demo(CustomerRepository repository, OrderRepository orderRepository, ProductRepository productRepository) {
        return (args) -> {

            Product p = new Product("衬衫", "A003");
            productRepository.save(p);

            OrderItem oi = new OrderItem(p, 3);


            Order o = new Order();

            o.getItems().add(oi);
            o = orderRepository.save(o);
            Optional<OrderItem> opt = o.getItems().findById("2343434343");
            opt.ifPresent(x -> System.out.println(x.getId()));
            final String[] itemid = {null};
            o.getItems().get().stream().findFirst().ifPresent(x -> {
                itemid[0] = x.getId();
                System.out.println(x.getId());
            });
            o.getItems().get().removeIf(x -> itemid[0].equals(x.getId()));



            o.getItems().add(new OrderItem(p, 5));
            o = orderRepository.save(o);
            Order o3 = orderRepository.findOne(o.getId());
            System.out.println(o3);


            // o2.getOrderitems().iterator().next().getProduct().getName();


            // save a couple of customers
            Customer c = new Customer("Jack", "Bauer");
            Address cs1 = new Address("chashan1");
            Address cs2 = new Address("chashan1");
            c.getAddressSet().add(cs1);
            repository.save(c);
            c.getAddressSet().add(cs2);
            c = repository.save(c);

            // repository.save(new Customer("Chloe", "O'Brian"));
            //repository.save(new Customer("Kim", "Bauer"));
            // repository.save(new Customer("David", "Palmer"));
            // repository.save(new Customer("Michelle", "Dessler"));

            // fetch all customers
            log.info("Customers found with findAll():");
            log.info("-------------------------------");
            for (Customer customer : repository.findAll()) {
                log.info(customer.toString());
            }
            log.info("");

            // fetch an individual customer by ID
            Customer customer = repository.findOne(1L);
            log.info("Customer found with findOne(1L):");
            log.info("--------------------------------");
            log.info(customer.toString());
            log.info("");

            // fetch customers by last name
            log.info("Customer found with findByLastName('Bauer'):");
            log.info("--------------------------------------------");
            //    for (Customer bauer : repository.findByLastName("Bauer")) {
            //       log.info(bauer.toString());
            //    }

            log.info("");
        };
    }

}